<?php
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.yhustc.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yhustc <yhustc@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * YBlog配置管理模块
 +------------------------------------------------------------------------------
 * @author    yhustc <yhustc@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */
import('@.Action.BaseAction');

class OptionsAction extends BaseAction
{
	/**
     +----------------------------------------------------------
     * 数据库与文件配置列表
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function index()
	{
		$optionDao = D("Options");
		$optionList = $optionDao->field("option_name,option_value")->findAll(); //在BaseAction中只取了autoload=yes的条目,这里要重新全取一遍
		
		$categoryDao = D("Category");
		$categoryList = $categoryDao->field("`cid`,`name`")->findAll();
		
		/*系统标签*/
		$this->assign("autoPostNameList",array("false"=>"不需要","true"=>"需要"));
		$this->assign("balanceTagList",array("0"=>"使用不同大小的Tag云图","1"=>"使用大小一样的Tag云图"));
		$this->assign("categoryList",$categoryDao->getCols($categoryList,"cid,name"));
		$this->assign("rssExcerptList",array("0"=>"输出全文","1"=>"输出摘要"));
		
		/*评论标签*/
		$this->assign("nameEmailList",array("0"=>"无须填写用户名和Email","1"=>"必须填写用户名和Email"));
		$this->assign("notifyList",array("0"=>"不发送邮箱通知","1"=>"发送邮箱通知"));
		$this->assign("statusList",array("open"=>"开启","closed"=>"关闭"));
		$this->assign("orderList",array("asc"=>"按时间升序排列","closed"=>"按时间倒序排列"));
		
		/*扩展标签*/
		$this->assign("urlModeList",array("1"=>"PATHINFO模式","2"=>"REWRITE伪静态","3"=>"兼容PATHINFO模式"));
		$user_config = require "user_config.php";
		foreach($user_config as $key=>$value)
		{
			if($value===true)
				$user_config[$key] = "true";
			if($value===false)
				$user_config[$key] = "false";
		}
		$this->assign("user_config",$user_config);
		$this->assign("boolStatusList",array("true"=>"是","false"=>"否"));
		
		$this->assign("pageFile","Options:index");
		$this->assign("options",$optionDao->getCols($optionList,"option_name,option_value")); //由于这个后赋值,会覆盖掉BaseAction中的options赋值
		$this->layout();
	}
	
	/**
     +----------------------------------------------------------
     * 保存配置到数据库和config文件
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function save()
	{
		//db_siteurl和EXTRA_UPLOAD_PATH这两个值如果用户在最后写上了/,把/去掉
		if(substr($_POST["db_siteurl"],strlen($_POST["db_siteurl"])-1) == '/')
           $_POST["db_siteurl"] = substr($_POST["db_siteurl"],0,strlen($_POST["db_siteurl"])-1);
		if(substr($_POST["EXTRA_UPLOAD_PATH"],strlen($_POST["EXTRA_UPLOAD_PATH"])-1) == '/')
           $_POST["EXTRA_UPLOAD_PATH"] = substr($_POST["EXTRA_UPLOAD_PATH"],0,strlen($_POST["EXTRA_UPLOAD_PATH"])-1);
        
		$optionDao = D("Options");
		$user_config = "<?php\r\nif (!defined('THINK_PATH')) exit();\r\nreturn array(\r\n";
		foreach($_POST as $key=>$value)
		{
			//setField返回的是update修改的行数,由于有的设置可能没有修改,结果有可能返回0,所以干脆不判断返回值
			if(strpos($key,"db_") === 0)
				$optionDao->setField("option_value",$value,"option_name='".substr($key,3)."'");	
			else if(strpos($key,"EXTRA_") === 0)
			{
				if(strtolower($value)=="true" || strtolower($value)=="false" || is_numeric($value))
					$user_config .= "\t'".substr($key,6)."'=>$value,\r\n";
				else
					$user_config .= "\t'".substr($key,6)."'=>'$value',\r\n";
			}
		}
		$user_config .= ");\r\n?>";

		file_put_contents("user_config.php",$user_config);
		//清空前台缓存
		clearBlogCache();
		//清空后台缓存
		clearCache(1);
		
		$this->success("修改配置成功");
	}
	
	/**
     +----------------------------------------------------------
     * 可选主题列表
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function themes()
	{
		$templateDir = BLOG_PATH."/Tpl/";
		$templateList = array();
		if (is_dir($templateDir)) {
		    if ($dh = opendir($templateDir)) {
		        while (($template = readdir($dh)) !== false) {
		        	if($template != "." && $template != ".." && filetype($templateDir.$template) == "dir" && file_exists_case($templateDir.$template."/screenshot.png"))
		            	$templateList[$template] = $template;
		        }
		        closedir($dh);
		    }
		}
		
		$this->assign("templateList",$templateList);
		$this->assign("user_config",require "user_config.php");
		$this->assign("pageFile","Options:themes");
		$this->layout();
	}
	
	/**
     +----------------------------------------------------------
     * 保存选用的模板
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function configTheme()
	{
		$user_config = require "user_config.php";
		$user_config["DEFAULT_TEMPLATE"] = $_POST["DEFAULT_TEMPLATE"];
		
		$config = "<?php\r\nif (!defined('THINK_PATH')) exit();\r\nreturn array(\r\n";
		foreach($user_config as $key=>$value)
		{
			if($value === true)
				$config .= "\t'".$key."'=>true,\r\n";
			else if($value === false)
				$config .= "\t'".$key."'=>false,\r\n";
			else if(is_numeric($value))
				$config .= "\t'".$key."'=>$value,\r\n";
			else
				$config .= "\t'".$key."'=>'$value',\r\n";
		}
		$config .= ");\r\n?>";
		
		file_put_contents("user_config.php",$config);
		//清空前台缓存
		clearBlogCache();
		//删除用于记录模板项的COOKIE值
		Cookie::delete("think_template");
		$this->success("模板设置成功");
	}
}
?>